package com.ruoyi.vote.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.vote.domain.Vote;
import com.ruoyi.vote.service.IVoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 投票信息Controller
 *
 * @author wxx
 * @date 2022-10-05
 */
@Api
@Controller
@RequestMapping("/vote/vote")
public class VoteController extends BaseController {
    private String prefix = "vote/vote";

    @Autowired
    private IVoteService voteService;

    @GetMapping()
    public String vote() {
        return prefix + "/vote";
    }

    /**
     * 查询投票信息列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Vote vote) {
        startPage();
        List<Vote> list = voteService.selectVoteList(vote);
        return getDataTable(list);
    }

    @GetMapping("/lists")
    @ResponseBody
    public List<Vote> lists() {
        return voteService.selectVoteList(null);
    }

    @GetMapping("/lists/{id}")
    @ResponseBody
    public Vote listById(@PathVariable Long id) {
        return voteService.selectVoteById(id);
    }

    @ApiOperation("通过用户名查询投票")
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult listByUsername(String username) {
        Vote vote = new Vote();
        vote.setUsername(username);
        List<Vote> votes = voteService.selectVoteList(vote);
        if (votes != null) {
            return AjaxResult.success("查询成功", votes);
        }
        return AjaxResult.error("查询失败");
    }

    /**
     * 新增投票信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存投票信息
     */

    @Log(title = "投票信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Vote vote) {
        return toAjax(voteService.insertVote(vote));
    }

    /**
     * 新增保存投票信息
     */
    @ApiOperation("新增保存投票信息")
    @Log(title = "投票信息", businessType = BusinessType.INSERT)
    @PostMapping("/add1")
    @ResponseBody
    public AjaxResult add(@RequestBody Vote vote) {
        int i = voteService.insertVote(vote);
        if (i > 0) {
            return AjaxResult.success("添加成功", vote.getId());
        }
        return AjaxResult.error("添加失败");
    }

    /**
     * 修改投票信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Vote vote = voteService.selectVoteById(id);
        mmap.put("vote", vote);
        return prefix + "/edit";
    }

    /**
     * 修改保存投票信息
     */
    @ApiOperation("修改保存投票信息")
    @Log(title = "投票信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Vote vote) {
        return toAjax(voteService.updateVote(vote));
    }

    @ApiOperation("修改保存投票信息")
    @Log(title = "投票信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit1")
    @ResponseBody
    public AjaxResult edit1(@RequestBody Vote vote) {
        return toAjax(voteService.updateVote(vote));
    }

    /**
     * 删除投票信息
     */
    @ApiOperation("删除投票信息")
    @Log(title = "投票信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(voteService.deleteVoteByIds(ids));
    }
}
